package org.bwgl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyReflect {
    public static void main(String[] args) {
        MyStudent myStudent = new MyStudent("张三", 10);
        Class aClass = myStudent.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field f :
                declaredFields) {
            f.setAccessible(true);
            try {
                System.out.println(f.get(myStudent));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method m:
             declaredMethods) {
            m.setAccessible(true);
            System.out.println(m.getName());
            if("mimi".equals(m.getName())){
                try {
                    m.invoke(myStudent,"测试");
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
